import sys
import threading
import time
import pygame
import random
import os

#初始化混音器
pygame.mixer.init()
music_list = []


def load_music():
    path = 'song/'
    for file in os.listdir(path):
        music_list.append(path + file)


def play_music(index):
    pygame.mixer.music.load(music_list[index])
    pygame.mixer.music.play()


load_music()
MUSIC_END_EVENT = pygame.USEREVENT + 1
pygame.mixer.music.set_endevent(MUSIC_END_EVENT)
current_music_index = 0
# 设置音乐音量（可选）
pygame.mixer.music.set_volume(0.5)
music_ended = False
music_next = False


def music_player():
    global current_music_index, music_ended, music_next
    play_music(current_music_index)
    while True:
        if music_ended or music_next:
            current_music_index = (current_music_index + 1) % len(music_list)
            play_music(current_music_index)
            music_ended = False
            music_next = False
        time.sleep(0.1)


# 创建并启动音乐播放线程
music_thread = threading.Thread(target=music_player)
music_thread.daemon = True
music_thread.start()

# 初始化pygame
pygame.init()
font = pygame.font.Font('font.ttf', 30)
pygame.font.init()
width, height = 810, 650
screen = pygame.display.set_mode((width, height))

# 颜色
color_gray = (200, 200, 200)
color_black = (0, 0, 0)
color_white = (255, 255, 255)
color_green = (0, 255, 0)
color_red = (255, 0, 0)


def show_message(message):
    text = font.render(message, True, color_red)
    text_rect = text.get_rect(center=(width // 2, 620))
    screen.blit(text, text_rect)


# 扫雷
class MineSweeping:
    def __init__(self):
        self.col = 10
        self.row = 10
        self.cell_size = 600 // self.col  # 单元格大小
        print(self.cell_size)
        self.grid_size = 10  # 网格大小
        self.mine_img = pygame.transform.scale(pygame.image.load("images/mine.png"), (self.cell_size, self.cell_size))
        self.flag_img = pygame.transform.scale(pygame.image.load("images/flag.png"), (self.cell_size, self.cell_size))
        # 保存被点击的单元格坐标的列表
        self.clicked_left = []
        self.clicked_right = []
        # 地雷数组
        self.array = list()
        # 地雷数量
        self.mine_number = 5
        # 提示地雷数量
        self.number_array = list()
        self.back = Button("回到菜单", (615, 500))
        self.hour = 0
        self.minute = 0
        self.second = 0
        self.time_show = False
        self.can_click = False
        self.hard1 = Button('10X10', (615, 100))
        self.hard2 = Button('15X15', (715, 100))
        self.hard3 = Button('20X20', (615, 200))
        self.hard4 = Button('25X25', (715, 200))
        self.hard_change = False
        self.time_thread = None

    def show_hard_button(self):
        text = font.render("选择难度", True, color_black)
        text_rect = text.get_rect(center=pygame.Rect(615, 30, 180, 40).center)
        screen.blit(text, text_rect)
        self.hard1.show()
        self.hard2.show()
        self.hard3.show()
        self.hard4.show()

    def handle_hard_change(self, event):
        if self.hard1.click(event):
            self.col = 10
            self.row = 10
            self.mine_number = 20
            self.load_image()
            self.reset()
        elif self.hard2.click(event):
            self.col = 15
            self.row = 15
            self.mine_number = 40
            self.load_image()
            self.reset()
        elif self.hard3.click(event):
            self.col = 20
            self.row = 20
            self.mine_number = 60
            self.load_image()
            self.reset()
        elif self.hard4.click(event):
            self.col = 25
            self.row = 25
            self.mine_number = 80
            self.load_image()
            self.reset()

    def load_image(self):
        self.cell_size = 600 // self.col
        self.mine_img = pygame.transform.scale(pygame.image.load("images/mine.png"), (self.cell_size, self.cell_size))
        self.flag_img = pygame.transform.scale(pygame.image.load("images/flag.png"), (self.cell_size, self.cell_size))

    def load_mine(self):
        data_list = []
        for index in range(self.row * self.col):
            if index < self.mine_number:
                data_list.append(1)
            else:
                data_list.append(0)
        random.shuffle(data_list)
        index = 0
        for o in range(self.row):
            temp = []
            number = []
            for p in range(self.col):
                temp.append(data_list[index])
                number.append(0)
                index += 1
            self.array.append(temp)
            self.number_array.append(number)
        for i in range(self.row):
            for j in range(self.col):
                if self.array[i][j] == 1:
                    continue
                left, right, top, bottom = False, False, False, False
                if i + 1 < self.row:
                    bottom = True
                if i - 1 >= 0:
                    top = True
                if j + 1 < self.col:
                    right = True
                if j - 1 >= 0:
                    left = True
                if top:
                    if self.array[i - 1][j] == 1:
                        self.number_array[i][j] += 1
                    if left and self.array[i - 1][j - 1] == 1:
                        self.number_array[i][j] += 1
                    if right and self.array[i - 1][j + 1] == 1:
                        self.number_array[i][j] += 1
                if left and self.array[i][j - 1] == 1:
                    self.number_array[i][j] += 1
                if right and self.array[i][j + 1] == 1:
                    self.number_array[i][j] += 1
                if bottom:
                    if self.array[i + 1][j] == 1:
                        self.number_array[i][j] += 1
                    if left and self.array[i + 1][j - 1] == 1:
                        self.number_array[i][j] += 1
                    if right and self.array[i + 1][j + 1] == 1:
                        self.number_array[i][j] += 1
        self.hour = 0
        self.minute = 0
        self.second = 0
        print("------------------游戏地雷信息------------------")
        for i in self.array:
            print(i)
        print("------------------游戏线索信息------------------")
        for i in self.number_array:
            print(i)

    def draw_line(self):
        for x in range(0, (self.col + 1) * self.cell_size, self.cell_size):
            pygame.draw.line(screen, color_black, (x, 0), (x, self.row * self.cell_size))
        for y in range(0, (self.row + 1) * self.cell_size, self.cell_size):
            pygame.draw.line(screen, color_black, (0, y), (self.col * self.cell_size, y))

    def fill_cell(self):
        for i in range(self.row):
            for j in range(self.col):
                if not self.clicked_left.__contains__((i, j)) and not self.clicked_right.__contains__((i, j)):
                    pygame.draw.rect(screen, color_gray,
                                     (j * self.cell_size, i * self.cell_size, self.cell_size, self.cell_size), width=0)
                elif self.clicked_right.__contains__((i, j)):
                    screen.blit(self.flag_img, (j * self.cell_size, i * self.cell_size))
                else:
                    if self.array[i][j] == 1:
                        screen.blit(self.mine_img, (j * self.cell_size, i * self.cell_size))
                    else:
                        # 渲染文本
                        text = font.render(str(self.number_array[i][j]), True, color_black)
                        if self.number_array[i][j] == 0:
                            text = font.render("", True, color_black)
                        # 获取文本的矩形并居中
                        text_rect = text.get_rect(
                            center=pygame.Rect(j * self.cell_size, i * self.cell_size, self.cell_size,
                                               self.cell_size).center)
                        # 将文本绘制到屏幕上
                        screen.blit(text, text_rect)

    def win(self):
        if len(self.clicked_left) == self.row * self.col - self.mine_number:
            return True
        if len(self.clicked_right) == self.mine_number and self.mine_number + len(
                self.clicked_left) == self.row * self.col:
            print("胜利")
            for i, j in self.clicked_right:
                if self.array[i][j] != 1:
                    return False
        else:
            return False
        return True

    @staticmethod
    def reset_button():
        pygame.draw.rect(screen, color_gray, (615, 300, 180, 40), width=0)
        text = font.render("重新开始", True, color_black)
        text_rect = text.get_rect(center=pygame.Rect(615, 300, 180, 40).center)
        screen.blit(text, text_rect)

    def reset(self):
        self.clear_old()
        self.load_mine()
        self.can_click = False
        if not self.time_thread.is_alive():
            self.time_show = True
            self.time_thread = TimeThread(self)
            self.time_thread.start()

    @staticmethod
    def click_reset(pos):
        x, y = pos
        if 600 <= x <= 780 and 300 <= y <= 340:
            return True
        return False

    def clear_old(self):
        self.clicked_left.clear()
        self.clicked_right.clear()
        self.array.clear()
        self.number_array.clear()

    def show_all(self):
        for i in range(self.row):
            for j in range(self.col):
                self.clicked_left.append((i, j))

    def show_zero(self, i, j):
        if self.array[i][j] != 1 and not self.clicked_left.__contains__((i, j)):
            self.clicked_left.append((i, j))
            if i - 1 >= 0 and self.number_array[i - 1][j] == 0:
                self.show_zero(i - 1, j)
            if j - 1 >= 0 and self.number_array[i][j - 1] == 0:
                self.show_zero(i, j - 1)
            if i + 1 < self.row and self.number_array[i + 1][j] == 0:
                self.show_zero(i + 1, j)
            if j + 1 < self.col and self.number_array[i][j + 1] == 0:
                self.show_zero(i, j + 1)

    def add_time(self):
        time.sleep(1)
        if self.second == 59:
            self.second = 0
            if self.minute == 59:
                self.minute = 0
                self.hour += 1
            self.minute += 1
        else:
            self.second += 1

    def show_time(self):
        pygame.draw.rect(screen, color_gray, (615, 380, 180, 40), width=0)
        text = font.render(f"用时 {self.hour}:{self.minute}:{self.second}", True, color_black)
        text_rect = text.get_rect(center=pygame.Rect(615, 380, 180, 40).center)
        screen.blit(text, text_rect)

    def main(self):
        pygame.display.set_caption("扫雷")
        global music_ended, music_next
        self.time_show = True
        self.can_click = False
        running = True
        self.time_thread = TimeThread(self)
        self.time_thread.start()
        self.load_mine()
        while running:
            for event in pygame.event.get():
                if event.type == MUSIC_END_EVENT:
                    music_ended = True
                if event.type == pygame.QUIT:
                    running = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER:
                        music_next = True
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    self.handle_hard_change(event)
                    x = event.pos[1] // self.cell_size
                    y = event.pos[0] // self.cell_size
                    if self.click_reset(event.pos):
                        self.reset()
                    if (self.back.x <= event.pos[0] <= self.back.x + self.back.size[0]
                            and self.back.y <= event.pos[1] <= self.back.y + self.back.size[1]):
                        self.clear_old()
                        print("返回")
                        self.time_show = False
                        return True
                    if x >= self.row or y >= self.col:
                        continue
                    if event.button == 1:
                        if self.array[x][y] == 1 and not self.clicked_right.__contains__((x, y)):
                            print("点到雷")
                            self.clicked_right.clear()
                            self.show_all()
                            self.can_click = True
                        elif not self.clicked_right.__contains__((x, y)):
                            if self.number_array[x][y] == 0:
                                self.show_zero(x, y)
                            elif not self.clicked_left.__contains__((x, y)):
                                self.clicked_left.append((x, y))
                    elif event.button == 3:
                        if self.clicked_right.__contains__((x, y)):
                            self.clicked_right.remove((x, y))
                        elif not self.clicked_left.__contains__((x, y)):
                            self.clicked_right.append((x, y))
            screen.fill((255, 255, 255))
            self.reset_button()
            self.show_time()
            self.show_hard_button()
            # 显示雷区
            self.fill_cell()
            # 绘制网格线
            self.draw_line()
            # 绘制返回按钮
            self.back.show()
            if self.can_click:
                self.time_show = False
                show_message("点到雷了，游戏结束")
            if self.win():
                show_message("恭喜你，游戏胜利！")
                self.time_show = False
            pygame.display.update()
        self.time_show = False
        pygame.quit()

    def handle_events(self, event):
        if self.main():
            return "StartPage"

    def render(self):
        pass


class TimeThread(threading.Thread):
    def __init__(self, mine_sweeping):
        threading.Thread.__init__(self)
        self.mine_sweeping = mine_sweeping

    def run(self):
        while self.mine_sweeping.time_show:
            self.mine_sweeping.add_time()


class Button:
    def __init__(self, text, pos):
        self.x, self.y = pos
        self.font = font
        self.text = self.font.render(text, True, pygame.Color("white"))
        self.size = self.text.get_size()
        self.surface = pygame.Surface(self.size)
        self.surface.fill((0, 123, 255))
        self.surface.blit(self.text, (0, 0))
        self.rect = pygame.Rect(self.x, self.y, self.size[0], self.size[1])

    def show(self):
        screen.blit(self.surface, (self.x, self.y))

    def click(self, event):
        x, y = pygame.mouse.get_pos()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if self.rect.collidepoint(x, y):
                return True
        return False


class GluttonousSnake:
    def __init__(self):
        self.back = Button('回到菜单', (600, 550))
        self.reset = Button('重新开始', (440, 550))
        self.running = True
        self.col = 44
        self.row = 35
        self.cell_size = 15
        self.line_color = (255, 0, 0)
        self.snake = None
        self.can_move = True
        self.snake_head_x = None
        self.snake_head_y = None
        self.snake_end_x = None
        self.snake_end_y = None
        self.direction = "right"
        self.lose = False
        self.lose_Message = None
        self.not_have_block = True
        self.block_x = None
        self.block_y = None
        self.pre = None
        self.snake_length = 0
        self.move_speed = 0.2
        self.gently = Button(' 缓慢 Q ', (680, 160))
        self.fast = Button(' 缓和 W ', (680, 220))
        self.faster = Button(' 稍快 E ', (680, 280))
        self.extremely_fast = Button(' 很快 R ', (680, 340))

    def show_speed(self):
        text = font.render('速度', True, color_black)
        screen.blit(text, text.get_rect(center=pygame.Rect(700, 50, 60, 100).center))
        self.gently.show()
        self.faster.show()
        self.fast.show()
        self.extremely_fast.show()

    def select_speed(self, event):
        if self.gently.click(event):
            self.move_speed = 0.2
        elif self.fast.click(event):
            self.move_speed = 0.15
        elif self.faster.click(event):
            self.move_speed = 0.1
        elif self.extremely_fast.click(event):
            self.move_speed = 0.05

    def init_snake(self):
        snake = []
        self.pre = []
        for i in range(self.row):
            temp = []
            a = []
            for j in range(self.col):
                temp.append(0)
                a.append(None)
            snake.append(temp)
            self.pre.append(a)
        snake[10][12] = 1
        snake[10][11] = 1
        snake[10][10] = 1
        self.snake_length += 3
        self.pre[10][11] = (10, 12)
        self.pre[10][10] = (10, 11)
        self.snake_head_x = 10
        self.snake_head_y = 12
        self.snake_end_x = 10
        self.snake_end_y = 10
        return snake

    def draw_snake(self):
        for i in range(self.row):
            for j in range(self.col):
                if self.block_x == i and self.block_y == j and not self.not_have_block:
                    pygame.draw.rect(screen, color_green,
                                     (j * self.cell_size, i * self.cell_size, self.cell_size, self.cell_size), width=0)
                elif self.snake[i][j] == 1:
                    pygame.draw.rect(screen, color_gray,
                                     (j * self.cell_size, i * self.cell_size, self.cell_size, self.cell_size), width=0)
                    pygame.draw.rect(screen, color_black,
                                     (j * self.cell_size, i * self.cell_size, self.cell_size, self.cell_size), width=1)
                else:
                    pass

    def start(self):
        pygame.display.set_caption("贪吃蛇")
        global music_ended,music_next
        self.running = True
        self.block_x = None
        self.block_y = None
        self.not_have_block = True
        self.lose = False
        self.direction = "right"
        self.snake = self.init_snake()
        self.can_move = True
        snake_move_thread = SnakeMoveThread(self)
        snake_move_thread.start()
        while self.running:
            for event in pygame.event.get():
                if event.type == MUSIC_END_EVENT:
                    music_ended = True
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER:
                        music_next = True
                if event.type == pygame.QUIT:
                    self.running = False
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    self.select_speed(event)
                    if self.back.click(event):
                        print("back")
                        self.can_move = False
                        return True
                    if self.reset.click(event):
                        self.snake = self.init_snake()
                        self.can_move = True
                        self.lose = False
                        self.move_speed = 0.2
                        self.not_have_block = True
                        self.direction = "right"
                        self.snake_length = 3
                        if not snake_move_thread.is_alive():
                            snake_move_thread = SnakeMoveThread(self)
                            snake_move_thread.start()
                elif event.type == pygame.KEYDOWN and not self.lose:
                    if event.key == pygame.K_UP:
                        if self.direction == "down":
                            continue
                        print("上")
                        self.direction = "up"
                    elif event.key == pygame.K_DOWN:
                        if self.direction == "up":
                            continue
                        print("下")
                        self.direction = "down"
                    elif event.key == pygame.K_LEFT:
                        if self.direction == "right":
                            continue
                        print("左")
                        self.direction = "left"
                    elif event.key == pygame.K_RIGHT:
                        if self.direction == "left":
                            continue
                        print("右")
                        self.direction = "right"
                    elif event.key == pygame.K_q:
                        self.move_speed = 0.2
                    elif event.key == pygame.K_w:
                        self.move_speed = 0.15
                    elif event.key == pygame.K_e:
                        self.move_speed = 0.1
                    elif event.key == pygame.K_r:
                        self.move_speed = 0.05
            if self.not_have_block:
                self.generate_block()
            screen.fill(color_white)
            self.draw_snake()
            self.back.show()
            self.reset.show()
            self.show_speed()
            self.show_snake_length()
            self.win()
            if self.lose:
                show_message(self.lose_Message)
            self.draw_border()
            pygame.display.update()

        pygame.quit()
        sys.exit()

    def win(self):
        if self.snake_length == 50:
            self.can_move = False
            show_message("恭喜你，游戏胜利！")

    def generate_block(self):
        i = random.randint(0, self.row - 1)
        j = random.randint(0, self.col - 1)
        while self.snake[i][j] == 1:
            j = random.randint(0, self.col - 1)
            i = random.randint(0, self.row - 1)
        self.snake[i][j] = 2
        self.block_x = i
        self.block_y = j
        self.not_have_block = False

    def show_snake_length(self):
        pygame.draw.rect(screen, color_white, (0, 525, 440, 87), width=0)
        text = font.render(f"当前蛇长：{self.snake_length}，还需{50 - self.snake_length}个格子", True, color_black)
        text_rect = text.get_rect(center=pygame.Rect(0, 525, 440, 87).center)
        screen.blit(text, text_rect)

    def eat_block(self):
        self.snake[self.block_x][self.block_y] = 1
        self.pre[self.snake_head_x][self.snake_head_y] = (self.block_x, self.block_y)
        self.snake_head_x = self.block_x
        self.snake_head_y = self.block_y
        self.not_have_block = True
        self.snake_length += 1

    def draw_border(self):
        pygame.draw.line(screen, self.line_color, (self.col * self.cell_size, 0),
                         (self.col * self.cell_size, self.row * self.cell_size))
        pygame.draw.line(screen, self.line_color, (0, self.row * self.cell_size),
                         (self.col * self.cell_size, self.row * self.cell_size))
        pygame.draw.line(screen, self.line_color, (0, 0),
                         (0, self.row * self.cell_size))
        pygame.draw.line(screen, self.line_color, (0, 0),
                         (self.col * self.cell_size, 0))

    def render(self):
        screen.fill(color_white)
        self.back.show()

    def handle_events(self, event):
        if self.start():
            return "StartPage"

    def snake_move(self):
        time.sleep(self.move_speed)
        if (self.snake_head_y == self.col - 1 and self.direction == "right" or
                self.snake_head_y == 0 and self.direction == "left" or
                self.snake_head_x == 0 and self.direction == "up" or
                self.snake_head_x == self.row - 1 and self.direction == "down"):
            self.can_move = False
            self.lose = True
            self.lose_Message = ' 撞到墙了，游戏结束！'
            return
        if (self.direction == "right" and self.snake_head_x == self.block_x and self.snake_head_y + 1 == self.block_y or
                self.direction == "left" and self.snake_head_x == self.block_x and self.snake_head_y - 1 == self.block_y or
                self.direction == "up" and self.snake_head_y == self.block_y and self.snake_head_x - 1 == self.block_x or
                self.direction == "down" and self.snake_head_y == self.block_y and self.snake_head_x + 1 == self.block_x):
            self.eat_block()
        if (self.direction == "right" and self.snake[self.snake_head_x][self.snake_head_y + 1] == 1 or
                self.direction == "left" and self.snake[self.snake_head_x][self.snake_head_y - 1] == 1 or
                self.direction == "up" and self.snake[self.snake_head_x - 1][self.snake_head_y] == 1 or
                self.direction == "down" and self.snake[self.snake_head_x + 1][self.snake_head_y] == 1):
            self.lose = True
            self.lose_Message = "吃到自己了，游戏结束！"
            self.can_move = False
        end_x, end_y = self.snake_end_x, self.snake_end_y
        self.snake[end_x][end_y] = 0
        self.snake_end_x = self.pre[end_x][end_y][0]
        self.snake_end_y = self.pre[end_x][end_y][1]
        self.pre[end_x][end_y] = None
        if self.direction == "right":
            self.pre[self.snake_head_x][self.snake_head_y] = (self.snake_head_x, self.snake_head_y + 1)
            self.snake[self.snake_head_x][self.snake_head_y + 1] = 1
            self.snake_head_y += 1
        elif self.direction == "left":
            self.pre[self.snake_head_x][self.snake_head_y] = (self.snake_head_x, self.snake_head_y - 1)
            self.snake[self.snake_head_x][self.snake_head_y - 1] = 1
            self.snake_head_y -= 1
        elif self.direction == "down":
            self.pre[self.snake_head_x][self.snake_head_y] = (self.snake_head_x + 1, self.snake_head_y)
            self.snake[self.snake_head_x + 1][self.snake_head_y] = 1
            self.snake_head_x += 1
        else:
            self.pre[self.snake_head_x][self.snake_head_y] = (self.snake_head_x - 1, self.snake_head_y)
            self.snake[self.snake_head_x - 1][self.snake_head_y] = 1
            self.snake_head_x -= 1


class SnakeMoveThread(threading.Thread):
    def __init__(self, snake):
        threading.Thread.__init__(self)
        self.snake = snake

    def run(self):
        while self.snake.can_move:
            self.snake.snake_move()


# 页面类
class StartPage:
    def __init__(self):
        self.mine_sweeping = Button("扫雷", (500, 400))
        self.gluttonous_snake = Button("贪吃蛇", (300, 400))

    def render(self):
        screen.fill(color_white)
        self.mine_sweeping.show()
        self.gluttonous_snake.show()
        show_tips()

    def handle_events(self, event):
        if self.mine_sweeping.click(event):
            return "MineSweeping"
        elif self.gluttonous_snake.click(event):
            return "GluttonousSnake"
        return None


def show_tips():
    font = pygame.font.Font('font.ttf', 30)
    text = font.render('游戏规则', True, color_black)
    screen.blit(text, text.get_rect(center=pygame.Rect(0, 0, 800, 100).center))
    font = pygame.font.Font('font.ttf', 24)
    text = font.render('贪吃蛇：按键盘上下左右键控制蛇移动在不撞到墙', True, color_black)
    screen.blit(text, text.get_rect(center=pygame.Rect(0, 100, 650, 50).center))
    text = font.render('不吃到自己的情况下蛇长达到50胜利', True, color_black)
    screen.blit(text, text.get_rect(center=pygame.Rect(0, 150, 510, 50).center))
    text = font.render('扫雷：鼠标右键标记地雷，再次右键可取消，左键打开安全格子', True, color_black)
    screen.blit(text, text.get_rect(center=pygame.Rect(0, 200, 800, 50).center))
    text = font.render('打开所有安全格子或者标记所有地雷并打开所有安全格子胜利', True, color_black)
    screen.blit(text, text.get_rect(center=pygame.Rect(0, 250, 780, 50).center))
    text = font.render('背景音乐：按回车键切换音乐', True, color_black)
    screen.blit(text, text.get_rect(center=pygame.Rect(0, 300, 440, 50).center))
    font = pygame.font.Font('font.ttf', 30)


class Game:
    def __init__(self):
        self.pages = {"StartPage": StartPage(), "MineSweeping": MineSweeping(), "GluttonousSnake": GluttonousSnake()}
        self.current_page = "StartPage"

    def run(self):
        running = True
        global music_ended,music_next
        while running:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER:
                        music_next = True
                if event.type == pygame.QUIT:
                    running = False
                result = self.pages[self.current_page].handle_events(event)
                if result:
                    print(result)
                    self.current_page = result
                if event.type == MUSIC_END_EVENT:
                    music_ended = True
            self.pages[self.current_page].render()
            pygame.display.flip()
            pygame.display.set_caption("ikun小游戏")
        pygame.quit()
        sys.exit()


if __name__ == '__main__':
    Game().run()
